/**
 * 督查小组服务层
 */
package net.huashitong.taskGroup.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.taskGroup.dao.JobGroupDao;
import net.huashitong.taskGroup.entity.JobGroup;
import net.huashitong.taskGroup.filter.JobGroupFilter;
import org.quartz.Job;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JobGroupService extends StringPKService<JobGroup> {
    @Autowired
    private JobGroupDao dao;
    @Autowired
    private SysTableService sysTableService;
    @Autowired
    private NewProjectsService newProjectsService;

    @Override
    protected BaseDao<JobGroup, String> getDao() {
        return dao;
    }

    /**
     * 查询所有督查组
     *
     * @param filter
     * @return
     */
   /* public List<JobGroup> getJobGroup(JobGroupFilter filter) {
        return dao.getJobInfo(filter);
    }*/


    @Override
    protected void beforeSave(JobGroup obj) {
        obj.setSysId(sysTableService.updateMaxSysId("t_jobgroup", null));
    }


    //保存督查小组
    public void saveGroup(JobGroupFilter filter) {
        JobGroup jg = new JobGroup();
        BeanUtils.copyProperties(filter, jg);
        save(jg);

    }

    /**
     * 根据项目ID查找该项目关联的督查组
     * @param projectId
     * @return
     */
    public List<JobGroup> queryGroupsByProjectId(String projectId) {
        List<JobGroup> jobGroups = new ArrayList<>();
        NewProjects newProjects = newProjectsService.getById(projectId);
        String groupCode = newProjects.getGroupCode();
        String[] groupCodeArr = groupCode.split(";");
        JobGroupFilter filter = new JobGroupFilter();
        for (int i = 0; i < groupCodeArr.length; i++) {
            filter.setGroupCode(groupCodeArr[i]);
            List<JobGroup> list = queryByFilter(filter);
            if (list.size() == 1) {
                jobGroups.add(list.get(0));
            }
        }
        return jobGroups;
    }


    public List<JobGroup> queryByFilter(JobGroupFilter filter) {
        List<JobGroup> list = dao.queryByFilter(filter);
        return list;
    }

    /**
     * 处理多个督查组
     * @param code
     * @return
     */
    public JobGroup getbyCode(String code)
    {
        JobGroup jobGroup  = new JobGroup();
        String tel="";
        String user="";
        String names ="";
        if(StringUtils.isNotBlank(code))
        {
            String[] arrCode =code.split(",");
            for(String item: arrCode)
            {
                JobGroup jobGroup1= dao.getJobInfobyCode(item.trim());
                tel=jobGroup1.getGroupTel()+","+tel;
                user=jobGroup1.getGroupContact()+","+user;
                names=jobGroup1.getGroupName()+","+names;
            }

        }
        jobGroup.setGroupTel(tel);
        jobGroup.setGroupContact(user);
        jobGroup.setGroupName(names);
        return jobGroup;
    }

    /**
     * 处理多个督查组
     * @param name
     * @return
     */
    public JobGroup getbyChart(String name)
    {
        JobGroup jobGroup  = new JobGroup();
        String tel="";
        String user="";
        if(StringUtils.isNotBlank(name))
        {
            String[] arrCode =name.split(",");
            for(String item: arrCode)
            {
                JobGroup jobGroup1= dao.getJobInfobyName(item.trim());
                tel=jobGroup1.getGroupTel()+","+tel;
                user=jobGroup1.getGroupContact()+","+user;
            }

        }
        jobGroup.setGroupTel(tel);
        jobGroup.setGroupContact(user);
        return jobGroup;
    }

}
